const app = getApp()
// import request from '../../utils/request.js'
// import {
//   getCreateOrderInfoApi,
// } from '../../api/index/order.js'
// import {
//   queryDefaultAddress
// } from '../../api/index/address.js'
// import {
//   queryProductInfoApi
// } from '../../api/index/product.js'
// var flag = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wechatUserId: '',
    showSuccessModal: false, // 控制模态框显示/隐藏
    showRedeem: false,
    confirmModal: false,
    productId: '',
    productStock: '',
    productStatus: '',
    imgUrl: "",
    receiverName: '',
    receiverPhone: '',
    freight: 0,
    redeemCode: '',
    orderId: '',
    productName: '',
    count: '1',
    totalPrice: '',
    addressId: '',
    receiverName: '',
    receiverPhone: '',
    receiverAddress: ''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (app.data.wechatUserId == '' || app.data.wechatUserId == null) {
      // wx.showModal({
      //   title: '您尚未登录到南罗岛小程序',
      //   content: '是否前往"我的"页面进行登录?',
      //   success: (res) => {
      //     if (res.confirm) {
      //       // 用户点击确认
      //       wx.switchTab({
      //         url: '/pages/my/my'
      //       });
      //     } else if (res.cancel) {
      //       // 用户点击取消
      //       wx.switchTab({
      //         url: '/pages/home/home'
      //       });
      //     }
      //   }
      // });
    }

    this.setData({
      productId: options.productId,
      wechatUserId: app.data.wechatUserId,
      totalPrice: options.totalPrice,
      productName: options.productName
    })
    this.getProductInfo()
    if (this.data.addressId == '') {
      this.getDefaultAddress()
    }
  },
  onShow() {
    console.log(this.data.addressId)

    // if (app.data.wechatUserId == '' || app.data.wechatUserId == null) {
    //   wx.showModal({
    //     title: '您尚未登录到南罗岛小程序',
    //     content: '是否前往"我的"页面进行登录?',
    //     success: (res) => {
    //       if (res.confirm) {
    //         // 用户点击确认
    //         wx.switchTab({
    //           url: '/pages/my/my'
    //         });
    //       } else if (res.cancel) {
    //         // 用户点击取消
    //         wx.switchTab({
    //           url: '/pages/home/home'
    //         });
    //       }
    //     }
    //   });
    // }
  },
  getDefaultAddress() {
    let param = {
      wechatUserId: this.data.wechatUserId
    }
    request.post(queryDefaultAddress, param).then((res) => {
      const resData = res.data.data;
      this.setData({
        addressId: resData.id,
        receiverName: resData.receiverName,
        receiverPhone: resData.receiverPhone,
        addressInfo: resData.receiverAddress,
      })
    })
  },
  getProductInfo() {
    let param = {
      id: this.data.productId,
    }
    request.post(queryProductInfoApi, param).then((res) => {
      const resData = res.data.data;
      const imgUrl = app.globalData.baseUrl + res.data.data.productCoverPath;
      this.setData({
        productName: resData.productName,
        imgUrl: imgUrl
      })
    })
  },
  onChooseAddress() {
    wx.navigateTo({
      url: '/pages/shippingAddress/shippingAddress?id=' + this.data.addressId,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    wx.setNavigationBarTitle({
      title: '创建订单'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },


  bindRedeemCode(e) {
    this.setData({
      redeemCode: e.detail.value
    })
  },
  submitOrder() {
    if (!flag) {
      return false
    }
    flag = false;
    let param = {
      buyCount: 1,
      memberId: this.data.wechatUserId,
      memberAddressId: this.data.addressId,
      productId: this.data.productId,
    }
    console.log(param)
    request.post(getCreateOrderInfoApi, param).then((res) => {
      flag = true;
      const resData = res.data;
      if (resData.code == '200') {
        this.setData({
          orderId: resData.data
        })
        wx.showToast({
            title: '创建成功',
            icon: 'none',
            success: () => {

            }
          }),
          this.showSuccessModal();
        // this.openOrderDetails();

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  //展示兑换弹窗
  showRedeem() {
    let addressId = this.data.addressId
    if (addressId == '') {
      wx.showModal({
        title: '您当前尚未选择收件地址,请选择地址后再进行操作',
        content: '是否跳转至选择地址页面进行操作?',
        success: (res) => {
          if (res.confirm) {
            // 用户点击确认
            this.onChooseAddress()
          } else if (res.cancel) {

          }
        }
      });
    } else {
      this.setData({
        showRedeem: true,
        confirmModal: true,
        showbg: true
      });
    }


  },
  // 关闭兑换弹窗
  closeRedeem: function () {
    if (this.data.showSuccessModal) {
      console.log(1)
    } else {
      this.setData({
        showRedeem: false,
        showbg: false,
        confirmModal: false,
      });
    }

  },
  // 订单创建成功后，显示模态框
  showSuccessModal: function () {
    this.setData({
      showSuccessModal: true,
      showRedeem: false,
      showbg: true
    });
  },

  // 关闭模态框
  closeSuccessModal: function () {
    this.setData({
      showSuccessModal: false,
      showRedeem: false,
      showbg: true
    });
  },
  //  切换为订单详情页
  openOrderDetails: function () {
    //跳转到订单列表页
    // let id = this.data.orderId;
    wx.redirectTo({
      // url: '/pages/orderDetails/orderDetails?id=' + id,
      url: '/pages/orderList/orderList'
    });
  },
  openHome: function () {
    wx.switchTab({
      url: '/pages/home/home',
    });
  },

})